/*
 * @(#)GSBaseForm.java 
 *
 * Project: BUS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   Jenny     2005-12-05    建立
 *   
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2005 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import system.LoginContext;

import com.hitrust.gs.bean.GSHandleRecord;
import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.dao.GSHandleRecordDAO;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.exception.GSException;

import framework.core.LogWriter;

/**
 * @author simonyi
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public abstract class GSBaseAction extends Action{
	/**
	 * 业务执行方法
	 * 
	 * @param form
	 * @return 处理结果导向页面标志
	 * @throws Exception
	 */
	public abstract String execute(GSBaseForm aForm) throws Exception;

	public final ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		GSBaseForm tGenForm = new GSBaseForm();
//		Properties tPro = System.getProperties();
//		tPro.list(System.out);
		if (form != null) {
			tGenForm = (GSBaseForm) form;
		}
		String tServletPath = request.getServletPath();// 格式是：“/A000000.do”
		System.out.println("path=" + tServletPath);
		String tTxCode = "";
		String tFuncCode = "";
		LoginContext tUserBean;
		String tUserID = "";
		String tUserName = "";
		// 如果不是登录、判断用户权限
//		if (!tServletPath.equals("/login.do")) {
			try{
			tTxCode = tServletPath.substring(2, 8);
			tFuncCode = tServletPath.substring(2, 6);

			 if (!tTxCode.equals("000201")){
			// 判断Session是否存在
			HttpSession tSession = request.getSession(false);
			if (tSession == null) {
				throw new GSException(GSException.E00002,
						GSException.E00002_MSG);// 用户连接超时，请重新登录系统
			}

			// 判断请求路径格式是否合法
			if (tServletPath == null || tServletPath.length() < 10) {
				throw new GSException("系统异常，用户请求路径非法。Servlet path = ["
						+ tServletPath + "]");
			}

			//判断用户信息是否存在
			tUserBean = (LoginContext) (tSession.getAttribute(GSBaseConstants.LOGIN));
			if (tUserBean == null) {
                if(request.getAttribute("window") != null){
                    request.setAttribute("window","true");
                }
				throw new GSException(GSException.E00002,
						GSException.E00002_MSG);
			}
//			tUserID = tUserBean.getGs_oper_id();
//			tUserName = tUserBean.getGs_oper_name();

			// 检查用户权限
            
//			if (!GSOperatorProcessor.checkRights(tFuncCode,tUserBean)) {
//				throw new GSException(GSException.E10033,
//						GSException.E10033_MSG);
//			}
//
//			// 保存用户请求的交易路径
//			tUserBean.addOperatedTrnx(tServletPath);
//			// }
//			// }else {
//			tTxCode = "00000101";
//			tFuncCode = "000001";
//			tUserID = ((GSLoginForm) form).getUserName();
             }
			 }
			catch(Exception e){
				e.printStackTrace();
				throw e;
			}
//		}
        
			// 执行交易处理
			tGenForm.setRequest(request);
			tGenForm.setResponse(response);
			tGenForm.getRequest().setAttribute("pageSize","10");
			tGenForm.getRequest().setAttribute("totalRecords","0");
			String tProcessResult = execute(tGenForm);

			// 记录日志
			if (true) {
				// if (TxProfileService.getInstance().recordOperLog(tTxCode)){
				GSLogWriter.info("操作员操作日志记录 = [" + tTxCode + "|" + tUserID
						+ "|" + tUserName + "|"
						+ DateUtil.getCurrentDate("yyyyMMdd HH:mm:ss") + "]");
			}

			response.setHeader("Cache-Control", "No-cache");
			response.setHeader("Cache-Control", "no-cache,no-store,max-age=0");
            response.setHeader("Pragma","no-cache");
			response.setDateHeader("Expires", 0);
//			response.resetBuffer();
			// 转发到对应的处理结果页面
			return mapping.findForward(tProcessResult);
	}
}
